<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        canvas {
            border: 1px solid red;
            display: block;
            margin: 100px auto;
        }
    </style>
</head>

<body>
    <canvas width="500px" height="500px"></canvas>
</body>
<script>
    var convas = document.querySelector("canvas")
    var paint = convas.getContext("2d");
    // paint.fillStyle = "red"
    // paint.fillRect(0, 0, 100, 100);
    convas.onmousedown = function (e) {
        // paint.beginPath();
        console.log(111)
        x = e.pageX - convas.offsetLeft;
        y = e.pageY - convas.offsetTop;
        console.log(x, y)
        paint.moveTo(x, y)
        paint.lineTo(x, y)
        paint.stroke();
        this.flag = true;
    }
    convas.onmousemove = function (e) {
        if (this.flag) {
            console.log(2)
            x = e.pageX - convas.offsetLeft;
            y = e.pageY - convas.offsetTop;

            paint.lineTo(x, y)
            paint.stroke();
            this.flag = true;
        }
    }
    document.onmouseup = function () {
        convas.flag = false;
    }
</script>

</html>